<?php
namespace app\modules\web\controllers;

use app\common\service\book\BookService;
use app\common\service\ConstantMapService;
use app\common\service\UrlService;
use app\common\service\UtilService;
use app\models\book\Book;
use app\models\book\BookCat;
use app\models\book\BookStockChangeLog;
use app\models\Images;
use app\modules\web\controllers\common\BaseController;
use Faker\Provider\bn_BD\Utils;

class BookController extends BaseController
{
    public $layout = 'main';
    //图书列表
    public function actionIndex()
    {
        $mix_kw = trim( $this->get('mix_kw','') );
        $status = intval( $this->get("status",ConstantMapService::$status_default) );
        $cat_id = intval( $this->get("cat_id",0) );

        $p = intval( $this->get('p',1) );
        $p = ($p > 0)?$p:1;

        $query = Book::find();

        if( $mix_kw ){
            $where_name = ['LIKE','name','%'.strtr( $mix_kw,['%'=>'\%','-'=>'\-','\\'=>'\\\\'] ).'%',false];
            $where_tags = ['LIKE','tags','%'.strtr( $mix_kw,['%'=>'\%','-'=>'\-','\\'=>'\\\\'] ).'%',false];
            $query->andWhere(['OR',$where_name,$where_tags]);
        }

        if( $status > ConstantMapService::$status_default ){
            $query->andWhere(['status'=>$status]);
        }

        if( $cat_id ){
            $query->andWhere( ['cat_id' => $cat_id] );
        }
        $total_res_count = $query->count();
        $total_page = ceil( $total_res_count / $this->page_size );


        $list = $query->orderBy([ 'id' => SORT_DESC ])
            ->offset( ($p -1) * $this->page_size )
            ->limit( $this->page_size )
            ->all();

        $cat_mapping = BookCat::find()->orderBy(['id' => SORT_DESC])->indexBy("id")->all();

        $data = [];

        if( $list ){
            foreach( $list as $_item ){
                $tmp_cat_info = isset( $cat_mapping[ $_item['cat_id'] ] )?$cat_mapping[ $_item['cat_id'] ]:[];

                $data[] = [
                    'id' => $_item[ 'id' ],
                    'name' => UtilService::encode( $_item['name'] ),
                    'price' => UtilService::encode( $_item['price'] ),
                    'stock' => UtilService::encode( $_item['stock'] ),
                    'tags' => UtilService::encode( $_item['tags'] ),
                    'status' => UtilService::encode( $_item['status'] ),
                    'cat_name' => $tmp_cat_info?UtilService::encode( $tmp_cat_info['name'] ):''
                ];
            }
        }
        return $this->render('index',[
            'list' => $data,
            'search_conditions' => [
                'mix_kw' =>$mix_kw,
                'p' => $p,
                'status' => $status,
                'cat_id' => $cat_id
            ],
            'status_mapping' =>ConstantMapService::$status_mapping,
            'cat_mapping' => $cat_mapping,
            'pages' => [
                'total_count' => $total_res_count,
                'page_size' => $this->page_size,
                'total_page' => $total_page,
                'p' => $p

            ]
        ]);
    }
    //图书编辑或添加
    public function actionSet()
    {
        if( \Yii::$app->request->isGet ){
            $id = intval( $this->get('id',0) );
            $info = [];
            if( $id ){
                $info = Book::find()->where([ 'id' => $id ])->one();
            }
            $cat_list = BookCat::find()->orderBy([ 'id' =>SORT_DESC ])->all();
            return $this->render('set',[
                'info' => $info,
                'cat_list' => $cat_list
            ]);

        }

        $id = intval( $this->post('id',0) );
        $cat_id = intval( $this->post('cat_id',0) );
        $name = trim( $this->post( 'name','' ) );
        $price = floatval( $this->post('price',0) );
        $main_image = trim( $this->post('main_image','') );
        $summary = trim( $this->post('summary','') );
        $stock = intval( $this->post( 'stock',0 ) );
        $tags = trim( $this->post('tags','') );
        $date_now = date("Y-m-d H:i:s");


        if( !$cat_id ){
            return $this->renderJson([],"请输入图书分类~~",-1);
        }

        if( mb_strlen( $name,"utf-8" ) < 1 ){
            return $this->renderJson([],"请输入符合规范的图书名称~~",-1);
        }

        if( $price <= 0 ){
            return $this->renderJson([],"请输入符合规范的图书价格~~",-1);
        }

        if( mb_strlen( $main_image ,"utf-8") < 3 ){
            return $this->renderJson( [],"请上传封面图~~",-1 );
        }

        if( mb_strlen( $summary  ,"utf-8") < 10 ){
            return $this->renderJson( [],"请输入图书描述，不能少于十个字符~~",-1 );
        }

        if( $stock < 1 ){
            return $this->renderJson([],"请输入符合规范的库存量~~",-1);
        }
        if( mb_strlen( $tags ,"utf-8" ) < 1 ){
            return $this->renderJson([],"请输入图书标签，便于搜索~~",-1);
        }

        $info = [];
        if( $id ){
            $info = Book::findOne(['id' => $id]);
        }

        if( $info ){
            $model_book = $info;
        }else{
            $model_book = new Book();
            $model_book->status = 1;
            $model_book->created_time = $date_now;
        }

        $before_stock = $model_book->stock;

        $model_book->cat_id = $cat_id;
        $model_book->name = $name;
        $model_book->price = $price;
        $model_book->main_image = $main_image;
        $model_book->summary = $summary;
        $model_book->stock = $stock;
        $model_book->tags = $tags;
        $model_book->updated_time = $date_now;

        if( $model_book->save(0) ){
            //库存变更
            BookService::setStockChangeLog( $model_book->id,( $model_book->stock - $before_stock ) );
        }

        return $this->renderJson( [],"操作成功~~" );
    }

    //图书详情
    public function actionInfo()
    {
        $id = intval( $this->get( "id",0 ) );
        $reback_url = UrlService::buildWebUrl( "/book/index" );

        if( !$id ){
            return $this->redirect( $reback_url );
        }

        $info = Book::find()->where([ 'id'=>$id ])->one();

        if( !$info ){
            return $this->redirect( $reback_url );
        }

        $stock_change_list = BookStockChangeLog::find()->where(['book_id' => $id])->orderBy(['id' => SORT_DESC])->all();
        return $this->render('info',[
            "info" => $info,
            "stock_change_list" => $stock_change_list
        ]);
    }
    //图片资源列表
    public function actionImages()
    {
        $p = intval( $this->get( 'p',1 ) );
        $p = ( $p > 0 )?$p:1;

        $bucket = "book";
        $query = Images::find()->where([ "bucket"=>$bucket ]);

        $total_res_count = $query->count();
        $total_page = ceil( $total_res_count / $this->page_size );

        $list = $query->orderBy(["id" => SORT_DESC])
            ->offset( ($p - 1) * $this->page_size )
            ->limit( $this->page_size )
            ->all();

        $data = [];
        if( $list ){
            foreach( $list as $_item ){
                $data[] = [
                    'url' => UrlService::buildPicUrl($bucket,$_item['file_key'])
                ];
            }
        }

        return $this->render('images',[
            'list' => $data,
            'pages' => [
                'total_count' => $total_res_count,
                'page_size' => $this->page_size,
                'total_page' => $total_page,
                'p' => $p
            ]
        ]);
    }

    //图书分类
    public function actionCat()
    {

        $status = intval( $this->get('status',ConstantMapService::$status_default) );
        $query = BookCat::find();

        if( $status > ConstantMapService::$status_default ){
            $query->where([ 'status' => $status ]);
        }

        $list = $query->orderBy(['weight'=>SORT_DESC,'id' => SORT_DESC])->all();
        return $this->render('cat',[
            'list' => $list,
            'status_mapping' => ConstantMapService::$status_mapping,
            'search_conditions' => [
                'status' => $status,
            ]
        ]);
    }

    //图书分类的添加或编辑
    public function actionCat_set()
    {
        if( \Yii::$app->request->isGet ){
            $id = intval( $this->get('id',0) );
            $info = [];
            if( $id ){
                $info = BookCat::find()->where( ['id' => $id] )->one();
            }
            return $this->render('cat_set',[
                'info' => $info
            ]);
        }

        $name = trim( $this->post( "name","" ) );
        $weight = intval( $this->post("weight",1) );
        $id = intval( $this->post( "id",0 ) );
        $date_now = date( "Y-m-d H:i:s" );

        if( mb_strlen( $name,"utf-8" ) < 1 ){
            return $this->renderJson( [],"请输入合法的分类名~~",-1 );
        }

        $has_in = BookCat::find()->where( ['name' => $name] )->andWhere( ['!=','id',$id] )->count();
        if( $has_in ){
            return $this->renderJson( [],"该分类名称已存在，请换一个试试~~",-1 );
        }

        $cat_info = BookCat::find()->where( ['id'=>$id] )->one();

        if( $cat_info ){
            $model_book_cat = $cat_info;
        }else{
            $model_book_cat = new BookCat();
            $model_book_cat->created_time = $date_now;
        }

        $model_book_cat->name = $name;
        $model_book_cat->weight = $weight;
        $model_book_cat->updated_time = $date_now;
        $model_book_cat->save(0);

        return $this->renderJson([],'操作成功~~');

    }

    public function actionOps()
    {
        if( \Yii::$app->request->isGet ){
            return $this->renderJson( [],ConstantMapService::$default_syserror );
        }

        $id = intval( $this->post( "id",0 ) );
        $act = trim( $this->post( "act","" ) );

        if( !$id ){
            return $this->renderJson([],"请选择要操作的账号~",-1);
        }

        if( !in_array( $act,['remove','recover'] ) ){
            return $this->renderJson( [],"操作有误，请重试~~",-1 );
        }

        $info = Book::find()->where([ 'id' => $id ])->one();
        if( !$info ){
            return $this->renderJson( [],"指定书籍不存在~",-1 );
        }

        switch( $act ){
            case 'remove':
                $info->status = 0;
                break;
            case 'recover':
                $info->status = 1;
                break;
        }

        $info->updated_time = date("Y-m-d H:i:s");
        $info->update(0);
        return $this->renderJson([],"操作成功~~" );

    }

    public function actionCat_ops()
    {
        if( !\Yii::$app->request->isPost ){
            return $this->renderJson([],ConstantMapService::$default_syserror,-1);
        }
        $id = intval( $this->post('id',0) );
        $act = trim( $this->post('act','') );

        if( !$id ){
            return $this->renderJson([],"请选择要操作的账号",-1);
        }

        if( !in_array( $act,['remove','recover'] ) ){
            return $this->renderJson([],"操作有误，请重试~~",-1);
        }

        $info = BookCat::find()->where(['id'=> $id])->one();
        if( !$info ){
            return $this->renderJson([],"指定分类不存在~~",-1);
        }

        switch ( $act ){
            case 'remove':
                $info->status = 0;
                break;
            case 'recover':
                $info->status = 1;
                break;
        }
        $info->updated_time = date("Y-m-d H:i:s");
        $info->update(0);
        return $this->renderJson( [],"操作成功~~" );
    }



}